package c.j.a.c;

import android.content.Context;
import android.content.IntentFilter;
import android.text.TextUtils;
import c.j.a.a.a.b;
import c.j.a.b.a.d;
import c.j.a.c.g$g.d.c;
import c.j.a.c.g.t;
import c.j.a.c.g.y;
import c.j.a.c.m.e;
import c.j.a.c.y.a.j;
import c.j.a.d.b.e.i;
import com.ss.android.socialbase.appdownloader.DownloadReceiver;
import java.util.ArrayList;
import java.util.Objects;

/* compiled from: TTDownloader */
public class z {
    public static volatile z f;
    public final b a = new r();
    public final x b = x.b();
    public final c.j.a.b.a.b c;
    public d d;
    public long e = System.currentTimeMillis();

    public z(Context context) {
        if (context == null || context.getApplicationContext() == null) {
            throw new IllegalArgumentException("Context is null");
        }
        y.a = context.getApplicationContext();
        i.a(y.a());
        c.a.f();
        c.j.a.d.a.i j = c.j.a.d.a.i.j();
        Context a = y.a();
        e eVar = new e();
        m.d dVar = new m.d(context);
        m mVar = new m();
        Objects.requireNonNull(j);
        j.a = eVar;
        j.b = dVar;
        j.c = mVar;
        if (!(a == null || c.j.a.d.a.i.l)) {
            String str = "application/vnd.android.package-archive";
            String str2 = c.j.a.d.b.b.e.a;
            if (!TextUtils.isEmpty(str)) {
                c.j.a.d.b.b.e.a = str;
            }
            c.j.a.d.b.e.b.g(a);
            c.j.a.d.b.e.b.j(new c.j.a.d.a.i.c());
            if (!c.j.a.d.a.i.m) {
                if (j.f == null) {
                    j.f = new DownloadReceiver();
                }
                try {
                    IntentFilter intentFilter = new IntentFilter();
                    intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
                    intentFilter.addAction("android.intent.action.BOOT_COMPLETED");
                    intentFilter.addAction("android.ss.intent.action.DOWNLOAD_COMPLETE");
                    IntentFilter intentFilter2 = new IntentFilter();
                    intentFilter2.addAction("android.intent.action.PACKAGE_ADDED");
                    intentFilter2.addAction("android.intent.action.PACKAGE_REPLACED");
                    intentFilter2.addDataScheme("package");
                    IntentFilter intentFilter3 = new IntentFilter();
                    intentFilter3.addAction("android.intent.action.MEDIA_MOUNTED");
                    intentFilter3.addDataScheme("file");
                    c.j.a.d.b.e.b.a().registerReceiver(j.f, intentFilter);
                    c.j.a.d.b.e.b.a().registerReceiver(j.f, intentFilter2);
                    c.j.a.d.b.e.b.a().registerReceiver(j.f, intentFilter3);
                    c.j.a.d.a.i.m = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            c.j.a.d.a.i.l = true;
        }
        c.j.a.d.a.i.j().d = new m.c();
        Objects.requireNonNull(c.j.a.d.a.i.j());
        Objects.requireNonNull(i.a(c.j.a.d.b.e.b.a()));
        context = c.j.a.d.b.e.b.a;
        c.j.a.d.b.e.b.Q = new l();
        c.j.a.d.a.i.j().h = new j();
        this.c = g.d();
    }

    public static z a(Context context) {
        if (f == null) {
            synchronized (z.class) {
                if (f == null) {
                    f = new z(context);
                }
            }
        }
        return f;
    }

    public void b(Context context, int i, c.j.a.a.a.c.e eVar, c.j.a.a.a.c.d dVar) {
        x xVar = this.b;
        Objects.requireNonNull(xVar);
        if (dVar != null && !TextUtils.isEmpty(dVar.a())) {
            t tVar = (t) xVar.c.get(dVar.a());
            if (tVar != null) {
                tVar.b(context).c(i, eVar).e(dVar).a();
            } else if (xVar.b.isEmpty()) {
                xVar.c(context, i, eVar, dVar);
            } else {
                synchronized (xVar) {
                    if (xVar.b.size() <= 0) {
                        xVar.c(context, i, eVar, dVar);
                    } else {
                        tVar = (t) xVar.b.remove(0);
                        tVar.b(context).c(i, eVar).e(dVar).a();
                        xVar.c.put(dVar.a(), tVar);
                    }
                }
            }
        }
    }

    public void c(String str, int i) {
        x xVar = this.b;
        Objects.requireNonNull(xVar);
        if (!TextUtils.isEmpty(str)) {
            t tVar = (t) xVar.c.get(str);
            if (tVar != null) {
                if (tVar.a(i)) {
                    xVar.b.add(tVar);
                    xVar.c.remove(str);
                }
                long currentTimeMillis = System.currentTimeMillis();
                if (currentTimeMillis - xVar.e >= 120000) {
                    xVar.e = currentTimeMillis;
                    if (!xVar.b.isEmpty()) {
                        currentTimeMillis = System.currentTimeMillis();
                        ArrayList arrayList = new ArrayList();
                        for (t tVar2 : xVar.b) {
                            if (!tVar2.b()) {
                                if (currentTimeMillis - tVar2.d() > 120000) {
                                    tVar2.g();
                                    arrayList.add(tVar2);
                                }
                            }
                        }
                        if (!arrayList.isEmpty()) {
                            xVar.b.removeAll(arrayList);
                        }
                    }
                }
            }
        }
    }

    public void d(String str, long j, int i, c.j.a.a.a.c.c cVar, c.j.a.a.a.c.b bVar) {
        x xVar = this.b;
        Objects.requireNonNull(xVar);
        if (!TextUtils.isEmpty(str)) {
            t tVar = (t) xVar.c.get(str);
            if (tVar != null) {
                tVar.a(cVar).f(bVar).d(null).a(j, i);
            }
        }
    }

    public void e(String str, boolean z) {
        x xVar = this.b;
        Objects.requireNonNull(xVar);
        if (!TextUtils.isEmpty(str)) {
            t tVar = (t) xVar.c.get(str);
            if (tVar != null) {
                tVar.a(z);
            }
        }
    }

    public d f() {
        if (this.d == null) {
            if (j.b == null) {
                synchronized (j.class) {
                    if (j.b == null) {
                        j.b = new j();
                    }
                }
            }
            this.d = j.b;
        }
        return this.d;
    }
}
